package dev.android.yeqh.iask_sina.common;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import dev.android.yeqh.iask_sina.BaseActivity;
import dev.android.yeqh.iask_sina.model.NetTaskParam;

public class NetTask extends AsyncTask<NetTaskParam, Long, Object>{
	BaseActivity context = null; 
	ProgressDialog pdialog = null;

	public NetTask(BaseActivity context, boolean showProgress) {
		this.context = context;
		
		pdialog = new ProgressDialog(context);
		pdialog.setTitle("Loading...");
        pdialog.setCancelable(true);
        pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        
        if (showProgress) {
        	pdialog.show();
        }

	}
	
	@Override
	protected Object doInBackground(NetTaskParam... params) {
		Object result = null;
		
		try {
			switch (params[0].getTaskType()) {
			case Constants.TYPE_GET_WORDS:
				result = Utils.getWords();
				break;
			case Constants.TYPE_SEARCH_RESULT:
				result = Utils.searchCidian((String)params[0].getParam()[0], (Integer)params[0].getParam()[1]);
				break;
			case Constants.TYPE_SHOW_DETAIL:
				result = Utils.getItemDetail((String)params[0].getParam()[0]);
				break;
			default:
				break;
			}
		} catch (Exception e) {
			result = e;
		}

		return result;
	}

	@Override
	protected void onPostExecute(Object result) {
		context.updateUI(result);
		pdialog.dismiss();
		
		super.onPostExecute(result);
	}
	
	
}
